package cacy.leetcode.linkedList;

/**
 * Project: LeetcodePractice
 * Package: cacy.leetcode.practice20250217
 * Class:   LinkedList03
 * Description:
 * 给定一个头节点为 head 的单链表用于记录一系列核心肌群训练编号，请将该系列训练编号 倒序 记录于链表并返回。
 *
 * 示例 1：
 * 输入：head = [1,2,3,4,5]
 * 输出：[5,4,3,2,1]
 *
 * 示例 2：
 * 输入：head = [1,2]
 * 输出：[2,1]
 *
 * 示例 3：
 * 输入：head = []
 * 输出：[]
 *
 * 提示：
 *     链表中节点的数目范围是 [0, 5000]
 *     -5000 <= Node.val <= 5000
 *
 * @Date: 2025/2/17 周一
 * @Time: 14:55
 * @Author: 凯齐
 */

class Solution03 {
    public ListNode trainningPlan(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode nodeLow = head;
        ListNode nodeNor = head.next;
        ListNode nodeFast = nodeNor;
        while (nodeNor != null) {
            nodeFast = nodeFast.next;
            if (nodeLow == head) {
                nodeLow.next = null;
            }
            nodeNor.next = nodeLow;
            nodeLow = nodeNor;
            nodeNor = nodeFast;
        }
        head = nodeLow;
        return head;
    }
}

public class LinkedList03 {
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.push(2);
        head.push(3);
        head.push(4);
        head = new Solution03().trainningPlan(head);
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
    }
}
